﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KOP.Services.App_Data;
using KOP.Services.ServiceLibrary;
using System.Configuration;
using System.IO;

namespace KOP.Services
{
    public partial class ManageTemplate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                foreach (string x in Enum.GetNames(typeof(TemplateType)))
                {
                    ctlTemplateType.Items.Add(x);
                }
                DataClassesDataContext dc = new DataClassesDataContext();
                var data = dc.Templates.Select(p => p);
                ctlDataGrid.DataSource = data;
                ctlDataGrid.DataBind();
            }
               
        }

        protected void ctlAdd_Click(object sender, EventArgs e)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            Template t = new Template();
            t.Type = ctlTemplateType.SelectedItem.Text;
            t.FilePath = ctlFilePath.Text.Trim();
            DateTime dt ;
            if (!DateTime.TryParse(ctlAddDate.Text.Trim(), out dt)) dt = DateTime.Now;
            t.DateTime = dt;
            t.Blob = ctlFileUpload.FileBytes;
            try
            {
                dc.Templates.InsertOnSubmit(t);
                dc.SubmitChanges();
                ctlDataGrid.DataBind();
                ctlResultLabel.Text ="İşlem başarıyla tamamlandı.";
            }
            catch(Exception ee)
            {
                ctlResultLabel.Text = "İşlem sırasında bir hata oluştu Hata :"+ee.Message.ToString();
            }
        }

        protected void ctlDataGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            Template info =
                (from item in dc.Templates
                where item.Id == (int)ctlDataGrid.SelectedValue
                select  item).First();
            ctlFilePath.Text = info.FilePath;
            ctlTemplateType.SelectedIndex = ctlTemplateType.Items.IndexOf(ctlTemplateType.Items.FindByText(info.Type));
            ctlAddDate.Text = info.DateTime.ToString();
           
        }

        protected void ctlUpdate_Click(object sender, EventArgs e)
        {
            if (ctlDataGrid.SelectedIndex >= 0)
            {
                DataClassesDataContext dc = new DataClassesDataContext();
                Template info =
                    (from item in dc.Templates
                     where item.Id == (int)ctlDataGrid.SelectedValue
                     select item).SingleOrDefault();
                if (ctlFileUpload.FileContent != null) info.Blob = ctlFileUpload.FileBytes;
                info.FilePath = ctlFilePath.Text;
                info.Type = ctlTemplateType.SelectedItem.Text;
                DateTime dt;
                info.DateTime = (string.IsNullOrEmpty(ctlAddDate.Text.Trim())) ? DateTime.Now : DateTime.TryParse(ctlAddDate.Text.Trim(), out dt) ? dt : DateTime.Now;
                dc.SubmitChanges();
                ctlDataGrid.DataBind();
                ctlResultLabel.Text = "İşlem başarıyla tamamlandı.";
            }
            else ctlResultLabel.Text = "Değiştme için bir kayıt seçin";
            
        }

        protected void ctlSearch_Click(object sender, EventArgs e)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            var data = dc.Templates.Select(p => p.FilePath.Contains(ctlSearch.Text));
            ctlDataGrid.DataSource = data;
            ctlDataGrid.DataBind();
        }
    }
}
